package com.klb.dao.impl;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.klb.base.DaoBase;
import com.klb.dao.UserDao;
import com.klb.model.BbsUser;
import com.klb.tools.Tools;

/**
 * Created by fujia on 2016/5/27.
 */
public class UserDaoImpl extends DaoBase implements UserDao {


    @Override
    public BbsUser getUserByTelephone(String telephone) {
    	BbsUser user = null;
    	
        if (!Tools.objectIsNullOrNot(telephone)){
            Session session = this.hibernateSessionFactory.getCurrentSession();
            Transaction tx = null;
            String sql = "from BbsUser user where user.telephone = :telephone";


            try {
                tx = session.beginTransaction();

                Query query = session.createQuery(sql);
                query.setParameter("telephone", telephone);

                user = (BbsUser) query.uniqueResult();

                tx.commit();

            }catch (Exception e){
                tx.rollback();
                e.printStackTrace();
            }

        }

        return user;
    }


    @Override
    public BbsUser getUserById(int userId) {
    	BbsUser user = null;
    	
        if (!Tools.objectIsNullOrNot(userId)){
            Session session = this.hibernateSessionFactory.getCurrentSession();
            Transaction tx = null;
            String sql = "from BbsUser user where user.userId = :userId";

            try {
                tx = session.beginTransaction();

                Query query = session.createQuery(sql);
                query.setParameter("userId", userId);

                user = (BbsUser) query.uniqueResult();

                tx.commit();

            }catch (Exception e){
                tx.rollback();
                e.printStackTrace();
            }
        }
        
        return user;
    }
    
    
}
